// Package controller @author dingrui @since 2021/11/29
package controller

import (
	"fmt"
	"strconv"
	"time"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/hero"

	"github.com/bannirui/mini-prize/common"
	"github.com/bannirui/mini-prize/config"
	"github.com/bannirui/mini-prize/dal/model"
	"github.com/bannirui/mini-prize/dal/service"
	"github.com/bannirui/mini-prize/dto"
	"github.com/bannirui/mini-prize/web/util/response"
)

func IndexRouter(party iris.Party) {
	var (
		party_     = party.Party("/index")
		controller = IndexController{}
	)
	party_.Get("/gifts", hero.Handler(controller.GetGifts))
	party_.Get("/new-prize", hero.Handler(controller.GetNewPrize))
	party_.Get("/my-prize", hero.Handler(controller.GetMyPrize))
	party_.Get("/login", hero.Handler(controller.GetLogin))
	party_.Get("/logout", hero.Handler(controller.GetLogout))
	party_.Get("/lucky", hero.Handler(controller.GetLucky))
}

type IndexController struct {
}

//
//  GetGifts
//  @Description:
//  @receiver c
//  @param ctx
//
func (c *IndexController) GetGifts(ctx iris.Context) {
	var (
		data     model.Gift
		dataList []model.Gift
		resList  []model.Gift
	)
	dataList = service.G_giftService.GetAll(true)
	resList = make([]model.Gift, 0)
	for _, data = range dataList {
		// 正常状态的才需要放进来
		if data.Status == 0 {
			resList = append(resList, data)
		}
	}
	response.Ok(ctx, response.CODE_OK, resList)
}

func (c *IndexController) GetNewPrize(ctx iris.Context) {
	var (
		gifts      []model.Gift
		gift       model.Gift
		giftIds    []int
		resultList []model.Result
	)
	gifts = service.G_giftService.GetAll(true)
	// 初始化
	giftIds = []int{}
	for _, gift = range gifts {
		// 虚拟券或者实物奖才需要放到外部榜单中展示
		if gift.Type > 1 {
			giftIds = append(giftIds, gift.Id)
		}
	}
	resultList = service.G_resultService.GetNewPrize(50, giftIds)
	response.Ok(ctx, response.CODE_OK, resultList)
	return
}

func (c *IndexController) GetMyPrize(ctx iris.Context) {
	var (
		// 用户登陆信息
		loginUser  *dto.LoginUserDTO
		resultList []model.Result
	)
	// TODO: 2021/11/29 鉴权 有效登陆用户才能抽奖
	// 只读取出来最新的100次中奖记录
	resultList = service.G_resultService.SearchByUser(loginUser.Uid, 1, 100)
	// 今天抽奖次数
	day, _ := strconv.Atoi(common.FormatFromUnixTimeShort(time.Now().Unix()))
	num := service.G_userDayService.Count(loginUser.Uid, day)
	response.Ok(ctx, response.CODE_OK, &response.MyPrizeVO{
		PrizeList: resultList,
		PrizeNum:  config.UserPrizeMax - num,
	})
	return
}

func (c *IndexController) GetLogin(ctx iris.Context) {
	var (
		loginUser *dto.LoginUserDTO
	)
	// 每次随机生成一个登录用户信息
	uid := common.Random(100000)
	loginUser = &dto.LoginUserDTO{
		Uid:      uid,
		Username: fmt.Sprintf("admin-%d", uid),
		Now:      common.NowUnix(),
		Ip:       common.GetClientIP(ctx.Request()),
	}
	response.Ok(ctx, response.CODE_OK, loginUser)
	return
}

func (c *IndexController) GetLogout() {
	// TODO: 2021/11/29 登出
}

func (c *IndexController) GetLucky(ctx iris.Context) {
	var (
		loginUser *dto.LoginUserDTO
	)
	// TODO: 2021/11/29 验证用户登陆状态
	if loginUser == nil || loginUser.Uid < 1 {
		response.Error(ctx, iris.StatusForbidden, response.CODE_PERMISSION_NIL)
		return
	}
	ip := common.GetClientIP(ctx.Request())
	api := &LuckyApi{}
	code, msg, gift := api.luckyDo(loginUser.Uid, loginUser.Username, ip)
	response.Ok(ctx, response.CODE_OK, &response.GetLuckVO{
		// TODO: 2021/11/29
		Code: code,
		Msg:  msg,
		Gift: gift,
	})
	return
}
